
import serial,time
'''

单次测量： 80 06 02 78
连续测量： 80 06 03 77
控制激光 ： 80 06 05 01 74 开启
80 06 05 00 75 关闭
设置量程 ： FA 04 09 05 F4 5m
FA 04 09 0A EF 10m
FA 04 09 1E DB 30m
FA 04 09 32 C7 50m
FA 04 09 50 A9 80m

'''
class Laser:
    def __init__(self):
        self.ser = serial.Serial("COM6", 9600, timeout=5)
    def close_ser(self):
        self.ser.close()
    def measure_once(self):
        cmd=[0x80, 0x06, 0x02, 0x78]
        self.ser.write(bytearray(cmd))  # write a string
        s=self.ser.read(1024)
        print ("read===",s)
        print ([chr(i) for i in s])
        for i,e in enumerate(s):
            if e==0x82:
                dis=s[i+1:-1]
                print (dis)
                res=[chr(i) for i in dis]
                str="".join(res)
                n=float(str)
                print (n)
                return n
        return 0
    def open_laser(self):
        cmd = [0x80, 0x06, 0x05,0x01, 0x74]
        self.ser.write(bytearray(cmd))
    def close_laser(self):
        cmd = [0x80, 0x06, 0x05, 0x00, 0x75]
        self.ser.write(bytearray(cmd))

    

if __name__ == '__main__':
    l=Laser()
    l.open_laser()
    time.sleep(2)
    l.close_laser()
    time.sleep(1)
    l.measure_once()
    l.close_ser()